package com.goldenlight.fvms.manager.controller;


import com.goldenlight.fvms.manager.entity.Company;
import com.goldenlight.fvms.manager.service.CompanyService;
import com.goldenlight.fvms.manager.vo.CompanyCollectInfo;
import com.goldenlight.fvms.manager.vo.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author luocx
 * @since 2018-12-23
 */
@RestController
@RequestMapping("/company")
public class CompanyController {

    @Autowired
    private CompanyService companyService;

    @PostMapping("/list")
    public Response getAll() {
        List<Company> list = companyService.list();
        List<CompanyCollectInfo> resultList = new ArrayList<>();
        list.stream().filter(item -> item.getPid() == 0).forEach(item -> {
            CompanyCollectInfo companyCollectInfo = new CompanyCollectInfo();
            companyCollectInfo.setValue(item.getId());
            companyCollectInfo.setLabel(item.getName());
            list.stream().filter(i->i.getPid().equals(item.getId())).collect(Collectors.toList()).stream().forEach(i->{
                if (companyCollectInfo.getChildren() == null) {
                    companyCollectInfo.setChildren(new ArrayList<>());
                }
                CompanyCollectInfo child = new CompanyCollectInfo();
                child.setValue(i.getId());
                child.setLabel(i.getName());
                companyCollectInfo.getChildren().add(child);
            });
            resultList.add(companyCollectInfo);
        });
        return Response.build(200, "success", resultList);
    }


}

